package com.example.communitysystem.controller;

import com.example.communitysystem.dto.QuestionDTO;
import com.example.communitysystem.mapper.QuestionMapper;
import com.example.communitysystem.mapper.UserMapper;
import com.example.communitysystem.model.Question;
import com.example.communitysystem.model.User;
import com.example.communitysystem.service.CommentService;
import com.example.communitysystem.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class AskQuestionController {

    private CommentService commentService;
    private QuestionService questionService;

    @GetMapping ("askquestion")
    public String askQuestion(){
        return "askquestion";
    }

    //用户提出问题
    @PostMapping("/askquestion")
    public String getQuestion(
            @RequestParam("title")String title,
            @RequestParam("description")String description,
            @RequestParam("tag")String tag,
            @RequestParam(value = "id", required = false)Long id,
            HttpServletRequest request,
            Model model
    ){
        //判断用户是否登录
        User user = null;
        user = (User) request.getSession().getAttribute("user");
        if(user == null){
            model.addAttribute("error","用户未登录！");
            return "askquestion";
        }
        //判断提交的问题信息，必填的需要回显给用户
        model.addAttribute("title",title);
        model.addAttribute("description",description);
        model.addAttribute("tag",tag);
        if(title == null || title == ""){
            model.addAttribute("error","标题不能为空！");
            return "askquestion";
        }
        if(description == null || description == ""){
            model.addAttribute("error","问题补充不能为空！");
            return "askquestion";
        }
        if(tag == null || tag == ""){
            model.addAttribute("error","标签不能为空！");
            return "askquestion";
        }



        Question question = new Question();
        question.setId(id);
        question.setTitle(title);
        question.setDescription(description);
        question.setTag(tag);
        question.setCreatorId(user.getId());
        question.setGmtCreate(System.currentTimeMillis());
        question.setGmtModified(question.getGmtCreate());

        //把问题添加或更新到数据库
        questionService.createOrUpdate(question);
        return "redirect:/";
    }

    //用户更新自己的问题信息
    @GetMapping("/askquestion/{id}")
    public String edit(@PathVariable(name = "id") Long id, Model model){
        QuestionDTO questionDTO = questionService.getQuestionById(id);

        model.addAttribute("id",questionDTO.getId());
        model.addAttribute("title",questionDTO.getTitle());
        model.addAttribute("description",questionDTO.getDescription());
        model.addAttribute("tag",questionDTO.getTag());

        return "askquestion";
    }

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    @Autowired
    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }
}
